#include "DL_OD.h"
#include "DL_MH.h"
#include "Type_Define.h"
#include "DL_ISDU.h"
#include "DL_MD.h"
#include "DL_Service.h"
#include "PAGE_SPACE.h"
#include "DL_EH.h"

OD_Service_TYPE OD_Service;
OD_STATE_TYPE OD_STATE;
uint8_t OD_Channel;
uint8_t Addr_FlowCtrl;
void Init_OD(void){
    OD_STATE=OD_Inactive_0;
}

//转移函数声明
void DL_OD_T2(void);

void DL_OD_T3(void);

void DL_OD_T4(void);

void DL_OD_T5(void);


//驱动事件

void Event_OD_ind(void){
    if(OD_STATE!=OD_Idle_1){
			  Event_MH_Ready();
        return;
    }
    
   OD_Channel=((*MC_STRUCT.MC)&0x60)>>5;
   Addr_FlowCtrl=(*MC_STRUCT.MC)&0x1F;
   
   if(OD_Channel==3){ //ISDU通道
      DL_OD_T4();
      return;
   }
   if(OD_Channel==2){ //诊断通道
       DL_OD_T5();
       return;
   }
   if(OD_Channel==1&&Addr_FlowCtrl==0&&MH_Dircetion==Direction_Write){  //命令通道
       DL_OD_T3();
       return;
   }
	 if(OD_Channel==1){  //Page通道
       DL_OD_T2();
       return;
   }else{  //OD_Channel==0 ，即表A.1的Process状态，这种状况不太可能发生，引文Process状态即Idle状态由ISDU处理
		      Event_MH_Ready();
        return;
		 
	 }

   
}


void Event_OH_Conf_ACTIVE(void){
   if(OD_STATE==OD_Inactive_0){
       OD_STATE=OD_Idle_1;
   }
}

void Event_OH_Conf_INACTIVE(void){
    if(OD_STATE==OD_Idle_1){
        OD_STATE=OD_Inactive_0;
    }
}

//Page通道处理
void DL_OD_T2(void){
	 
    uint8_t PageAddr=(*MC_STRUCT.MC)&0x1F;
   if(MH_Dircetion==Direction_Read){ //Page读
        if(Page_RW_Type[PageAddr]&R==R){
            *MC_STRUCT.OD=*Page_Space[PageAddr];
            Event_MH_Ready();
            
        }
   }else{ //Page写
        if(Page_RW_Type[PageAddr]&W==W){
            *Page_Space[PageAddr]=*MC_STRUCT.OD;
            Event_MH_Ready();
            //待更新Service_AL_Write_rsp();
        }
   }
}

//命令通道
void DL_OD_T3(void){
   //直接在此处处理命令
   uint8_t MasterCommand=*MC_STRUCT.OD;
   if(MasterCommand==0x5A){
       Event_MCmd_FALLBACK();
       return;
   }
   if(MasterCommand==0x97){
       Event_MCmd_STARTULP();
       return;
   }
   if(MasterCommand==0x9A){
       Event_MCmd_PREOPRATE();
       return;
   }
   if(MasterCommand==0x99){
       Event_MCmd_OPERATE();
       Service_DL_Control_ind(PDOUTINVALID);
       return;
   }
   if(MasterCommand==0x98){
       Service_DL_Control_ind(PDOUTVALIDE);
       return;
   }
    
}
 


//此函数暗示了都是ISDU
void DL_OD_T4(void){
    //1 MH_Direction 
    //2 Channel
    //3 Address(FlowCtrl)
    if(*MC_STRUCT.MC==0xF1){ //IDEL  发送结束 或者Idle状态
        Event_ISDU_Idle();
        return;
    }
    else if(*MC_STRUCT.MC==0x70){ //开始接收 
        Event_ISDUStart();
        return;
    }
    else if(((*MC_STRUCT.MC)&0xF0)==0x60){ //开始写
        Event_ISDUWrite();
        return;
    }
		else if(*MC_STRUCT.MC==0xF0){ //ISDURecCOmplete事件或者ISDURead(ISDUWait状态)事件
			  Event_ISDURecComplete_Or_Read(); 
			  return;
			
		}		
    
    else if(((*MC_STRUCT.MC)&0xF0)==0xE0){  
       Event_ISDURead();
       return;
    }else if(((*MC_STRUCT.MC)&0x1F)==0x1F){
       Event_ISDUAbort();
       return;
    } 
     else{
         Event_ISDUError_BOOL();
         return;
     }
}

void DL_OD_T5(void){
    Event_EventRead();
}
